package com.ccjq.jq173.shosestore.shoppingcart.dao.impl;

import com.ccjq.jq173.shosestore.core.db.manager.DbConnection;
import com.ccjq.jq173.shosestore.core.util.UuidUtils;
import com.ccjq.jq173.shosestore.shoppingcart.dao.ShoppingcartDao;
import com.ccjq.jq173.shosestore.shoppingcart.po.ShoppingCart;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import sun.security.pkcs11.Secmod;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static com.ccjq.jq173.shosestore.core.util.UuidUtils.generateShotUUID;

public class ShoppingcartDaoImpl implements ShoppingcartDao
{
    @Override
    public ShoppingCart queryCar(String productId, String userId)
    {
        ShoppingCart cart = null;
        Connection con = null;
        PreparedStatement pre = null;
        ResultSet res =null;
        try
        {
            String sql ="select * from t_shopping_cart where shopping_cart_id=? and user_id=?";
            con = DbConnection.getConnection();
            pre = con.prepareStatement(sql);
            pre.setString(1,productId);
            pre.setString(2,userId);
            res = pre.executeQuery();
            if(res.next())
            {
                cart=new ShoppingCart();
                cart.setShoppingCartId(res.getString(1));
                cart.setProductId(res.getString(2));
                cart.setQuantity(res.getInt(3));
                cart.setUserId(res.getString(4));
            }
        }
        catch (SQLException e)
        {
            System.out.println("查询指定购物车鞋的信息失败");
            e.printStackTrace();
        }
        finally
        {
            DbConnection.close(res,pre,con);
        }
        System.out.println("cart:"+cart);
        return cart; //返回的是购物车id
    }

    @Override
    public boolean addCar(String productId, int quantity, String userId)
    {
        boolean b=false;
        Connection con = null;
        PreparedStatement pre = null;
        try
        {
            String shoppingcartuuid = UuidUtils.generateShotUUID("u",10);
            System.out.println("打印添加购物车的dao包中的shop="+shoppingcartuuid);
            String sql ="insert into t_shopping_cart values('"+shoppingcartuuid+"',?,?,?)";
            con = DbConnection.getConnection();
            pre = con.prepareStatement(sql);
            pre.setString(1,productId);
            pre.setInt(2,quantity);
            pre.setString(3,userId);
            b=pre.executeUpdate()>0;
        }
        catch (SQLException e)
        {
            System.out.println("添加指定购物车鞋的信息失败");
            e.printStackTrace();
        }
        finally
        {
            DbConnection.close(null,pre,con);
        }
        return b;
    }

    @Override
    public boolean updateCar(String shoppingCartId, int quantity)
    {
        boolean b=false;
        Connection con = null;
        PreparedStatement pre = null;
        try
        {
            String sql ="update t_shopping_cart set quantity=? where shopping_cart_id=?";
            con = DbConnection.getConnection();
            pre = con.prepareStatement(sql);
            pre.setInt(1,quantity);
            pre.setString(2,shoppingCartId);
            b=pre.executeUpdate()>0;
        }
        catch (SQLException e)
        {
            System.out.println("更新指定购物车鞋的信息失败");
            e.printStackTrace();
        }
        finally
        {
            DbConnection.close(null,pre,con);
        }
        return b;
    }

    @Override
    public boolean removeCarShoes(String shoppingCartId)
    {
        boolean b=false;
        Connection con = null;
        PreparedStatement pre = null;
        try
        {
            String sql ="delete from t_shopping_cart where shopping_cart_id=?";
            con = DbConnection.getConnection();
            pre = con.prepareStatement(sql);
            pre.setString(1,shoppingCartId);
            b=pre.executeUpdate()>0;
        }
        catch (SQLException e)
        {
            System.out.println("删除指定购物车鞋的信息失败");
            e.printStackTrace();
        }
        finally
        {
            DbConnection.close(null,pre,con);
        }
        return b;
    }

    @Override
    public boolean shanchuCarShoes(String shoppingCarId, String userId,Connection con) {
        boolean b=false;
        Connection conn=DbConnection.getConnection();
        PreparedStatement pre = null;
        try
        {
            System.out.println("删除的shoppingcartID："+shoppingCarId);
            System.out.println("删除的userID："+userId);
            String sql ="delete  t_shopping_cart where shopping_cart_id=? and user_id=?";
            pre = conn.prepareStatement(sql);
            pre.setString(1,shoppingCarId);
            pre.setString(2,userId);
            b=pre.executeUpdate()>0;
        }
        catch (SQLException e)
        {
            System.out.println("删除指定购物车鞋的信息失败");
            e.printStackTrace();
        }
        finally
        {
            DbConnection.close(pre);
        }
        return b;
    }

    @Override
    public List<ShoppingCart> queryAllCarShoes(String userId)
    {
        List<ShoppingCart> list = new ArrayList<ShoppingCart>();
        Connection con = null;
        PreparedStatement pre = null;
        ResultSet res =null;
        try
        {
            String sql ="select * from t_shopping_cart where user_id=?";
            con = DbConnection.getConnection();
            pre = con.prepareStatement(sql);
            pre.setString(1,userId);
            res = pre.executeQuery();
            while(res.next())
            {
                ShoppingCart shopcar = new ShoppingCart();
                shopcar.setShoppingCartId(res.getString(1));
                shopcar.setProductId(res.getString(2));
                shopcar.setQuantity(res.getInt(3));
                shopcar.setUserId(res.getString(4));
                list.add(shopcar);
            }
        }
        catch (SQLException e)
        {
            System.out.println("查询购物车全部信息失败");
            e.printStackTrace();
        }
        finally
        {
            DbConnection.close(res,pre,con);
        }
        return list;
    }
}
